import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NavigatorLink } from '@app/shared/models/models/navigator-link';
import { NavigatorLevel, NAVIGATOR_LIST } from './models/navigator-list';

@Component({
  selector: 'app-navigator',
  templateUrl: './navigator.component.html',
  styleUrls: ['./navigator.component.less']
})
export class NavigatorComponent implements OnInit {

  public navigateLinks: NavigatorLink[];
  public navigatorLevel = NavigatorLevel;

  constructor(private router: Router) {
    this.navigateLinks = NAVIGATOR_LIST;
  }

  ngOnInit() {
    this.activeLink();
  }

  /**
   * ---------------------------------------------------------------------------
   * @NOTE 頁面連結
   * ---------------------------------------------------------------------------
   */

  /**
   * 激活連結
   *
   * @method private
   */
  private activeLink(): void {
    const [root, moduleType, linkType] = this.router.url.split('/');

    this.navigateLinks = this.navigateLinks.map(link => {
      link.active = link.key === linkType;
      return link;
    });
  }

  /**
   * 導覽特定頁面
   *
   * @method public
   * @param link 連結路由名稱
   */
  public navigate(link: string): void {
    this.navigateLinks = this.navigateLinks.map(link1 => {
      link1.active = link1.key === link;
      return link1;
    });
    this.router.navigate([`main/${link}`]);
  }
}
